﻿using System.Web.Mvc;
using TMT_CMS.BL;
using TMT_CMS.BL.DB;
using TMT_CMS.BL.Model;
using TMT_CMS.BL.Model.User;
using TMC_CMS.Utility;

namespace TMC_CMS.Controllers
{
    public class UserController : Controller
    {
        
        public ActionResult Index(UserSearchModel searchModel, PaggingRequestModel paggingModel)
        {
            var result = UserManager.GetUserList(searchModel, paggingModel);
            return View(result);
        }

        public ActionResult UserRoleCreate()
        {
           UserRoleViewModel userRole = new UserRoleViewModel();
            userRole.Permissions = UserManager.GetPrermissionList(0);
            userRole.RoleName = "";
            return View(userRole);
        }

        //[Authorize]
        [HttpPost]
        public ActionResult UserRoleCreate(UserRoleEditModel roleView)
        {
            if (UserManager.CreateUserRole(roleView.RoleName, roleView.PermissionIDs))
            {
                this.AddMessage(string.Format("操作成功：创建用户组【{0}】成功.", roleView.RoleName));
            }
            else
            {
                this.AddError(string.Format("操作失败，创建用户组【{0}】失败", roleView.RoleName));
            }
            return RedirectToAction("UserRoleList");
        }

        /// <summary>
        /// 检查用户组名称是否已经存在
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public ActionResult CheckRoleNameExists(string roleName,int ID)
        {
            bool exists = UserManager.CheckRoleNameExists(roleName, ID);
            return Json(!exists, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UserDelete(int id)
        {
            UserInfoEditModel editModel = new UserInfoEditModel();
            UserInfo user = UserManager.GetUserInfo(id);
            if (user != null)
            {
                user.ID = id;
                editModel.Email = user.Email;
                editModel.FamilyName = user.FamilyName;
                editModel.RoleIDs = user.RoleID;
                editModel.UserName = user.Username;
                SelectList roles = new SelectList(UserManager.GetRoleInfoList(), "ID", "Name", user.RoleID);
                ViewData["RoleIDs"] = roles;
            }
            return View(editModel);
        }

        public ActionResult UserConfirmDelete(int id)
        {
            UserManager.DeleteUserInfo(id);
            this.AddMessage(string.Format("操作成功：删除用户成功，用户编号【{0}】.", id));
            return RedirectToAction("Index", "User");
        }

        public ActionResult UserDetail(int id)
        {
            UserInfoEditModel editModel = new UserInfoEditModel();
            UserInfo user = UserManager.GetUserInfo(id);
            if (user!=null)
            {
                user.ID = id;
                editModel.Email = user.Email;
                editModel.FamilyName = user.FamilyName;
                editModel.RoleIDs = user.RoleID;
                editModel.UserName = user.Username;
                SelectList roles = new SelectList(UserManager.GetRoleInfoList(), "ID", "Name", user.RoleID);
                ViewData["RoleIDs"] = roles;
            }

            return View(editModel);
        }

        [HttpPost]
        public ActionResult UserDetail(UserInfoEditModel editModel)
        {
            UserManager.UpdateUser(editModel);
            this.AddMessage(string.Format("操作成功：更新用户信息成功，用户名【{0}】.",editModel.UserName));
            return RedirectToAction("Index", "User");
        }

        public ActionResult CheckUserNameExists(string UserName, int ID)
        {
            bool exists = UserManager.CheckUserNameIsExist(ID, UserName);
            return Json(!exists, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UserRoleDetail(int id)
        {
            UserRoleViewModel userRole = new UserRoleViewModel();
            userRole.Permissions = UserManager.GetPrermissionList(id);
            RoleInfo roleInfo = UserManager.GetRoleInfo(id);
            userRole.RoleName = "";
            if (roleInfo!=null)
            {
                userRole.RoleName = roleInfo.Name;
            }
            
            return View(userRole);
        }

        [HttpPost]
        public ActionResult UserRoleUpdate(UserRoleEditModel roleView)
        {
            UserManager.UserRoleUpdate(roleView);
            this.AddMessage(string.Format("操作成功：更新用户组【{0}】信息成功.", roleView.RoleName));

            return RedirectToAction("UserRoleList", "User", new {ID = roleView.ID});
        }

        public ActionResult UserRoleList(UserRoleSearchModel userRoleSearchModel,PaggingRequestModel paggingRequestModel )
        {
            PaggingResultModel<RoleInfo, UserRoleSearchModel> roleInfos = UserManager.GetRolesInfoList(userRoleSearchModel, paggingRequestModel);
            return View(roleInfos);
        }

        public ActionResult UserRoleDelete(int id)
        {
            return View(UserManager.UserRoleDelete(id));
        }

        [HttpPost]
        public ActionResult UserRoleConfirmDelete(int id)
        {
            //检查是否有用户是此分组下
            if (UserManager.CheckIsRoleHasUserInfo(id))
            {
                this.AddError(string.Format("删除失败：此用户组下还有用户，不能删除."));
                return RedirectToAction("UserRoleDelete", new {ID = id});
            }
            else
            {
                UserManager.DeleteRoleInfo(id);
                this.AddMessage(string.Format("操作成功：删除用户组成功，用户组编号{0}.", id));

                return RedirectToAction("UserRoleList", "User");
            }

        }
        
        public ActionResult UserCreate()
        {
            UserInfoViewModel viewModel = new UserInfoViewModel();
            SelectList roles = new SelectList(UserManager.GetRoleInfoList(),"ID","Name");
            ViewData["RoleIDs"] = roles;
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult UserCreate(UserInfoViewModel infoViewModel)
        {
            UserManager.CreateUser(infoViewModel);
            this.AddMessage(string.Format("操作成功：创建用户成功，用户名为【{0}】.", infoViewModel.UserName));
            return RedirectToAction("Index", "User");
        }
    }
}
